package edu.mtu.citizenscience.mushroommapper.activity.main;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
import edu.mtu.citizenscience.mushroommapper.MMActivity;
import edu.mtu.citizenscience.mushroommapper.activity.R;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Tutorial Activity displays slides to show the user how the app works
 */
public class TutorialActivity extends MMActivity implements ViewFactory {
	// slides to display portrait
    Integer[] slides_p = {
    	R.drawable.tutorial_0,
    	R.drawable.tutorial_1,
    	R.drawable.tutorial_2
    };
	// slides to display horizontal
    Integer[] slides_h = {
        R.drawable.tutorial_0_h,
        R.drawable.tutorial_1_h,
        R.drawable.tutorial_2_h
    };
    // currently displayed slides
    Integer[] slides;
    
    // the current slide position starts at 0
    private int position = 0;
    // image switcher which changes between the images
	private ImageSwitcher slideSwitcher;

	/**
	 * Tutorial Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        this.setContentView(R.layout.tutorial_activity);
        
        // check if the orientation is portrait
        if (getResources().getConfiguration().orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
        {
        	// set the slides as the portrait orientation
        	slides = slides_p;
        }
        else
        {
        	// set the slides as the horizontal orientation
        	slides = slides_h;
        }
        
        // Create ImageSwitcher to cycle through the tutorial slides
        slideSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
        slideSwitcher.setFactory(this);
        // set the animations for removing and bringing photos
        slideSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        slideSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        
        // update page view
        this.updatePage();
        
        // set click handlers for prev and next button
        this.aq.id(R.id.prev_button).clicked(this, "prevClicked");
        this.aq.id(R.id.next_button).clicked(this, "nextClicked");
    }

	/**
	 * Make a new image view
	 * 
	 * @param   void
	 * @return	View - the new image view
	 */
	public View makeView() 
	{
		return new ImageView(this);
	}
	
	/**
	 * onClick handler for the prev button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void prevClicked()
	{
		// deincrement the image position
		this.position--;
		// update the page display
		this.updatePage();
	}
	
	/**
	 * onClick handler for the next button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void nextClicked()
	{
		// increment the image position
		this.position++;
		// update the page display
		this.updatePage();
	}
	
	/**
	 * update the page display
	 * 
	 * @param   void
	 * @return	void
	 */
	private void updatePage()
	{
		// check if the position is 0
		if (this.position == 0)
		{
			// set the prev button to invisible as there is no prev image
			this.aq.id(R.id.prev_button).invisible();
		}
		// check if position is the last slide
		else if (position == (slides.length - 1))
		{
			// set the next button to invisible as there is no next image
			this.aq.id(R.id.next_button).invisible();
		}
		else
		{
			// set both prev and next button as visible
			this.aq.id(R.id.prev_button).visible();
			this.aq.id(R.id.next_button).visible();
		}
		
		// change image on the switcher
		this.slideSwitcher.setImageResource(this.slides[this.position]);
		
		// set the page number text to the current image position out of all slides
		this.aq.id(R.id.tutorial_pg).text((this.position + 1) + "/" + this.slides.length);
	}

}
